//
//  OrderSectionController.swift
//  FeastBooking
//
//  Created by Imon on 2017/7/15.
//  Copyright © 2017年 Imon. All rights reserved.
//

import Foundation
import UIKit
import IGListKit

protocol HistoryOrderSectionControllerDelegate {
    func showOrderDetail(order: UserOrder, role: [RoleInOrder])
}

class HistoryOrderSectionController: ListSectionController {
    
    var historyOrder: UserOrder!
    
    var delegate: HistoryOrderSectionControllerDelegate?
    
    override init() {
        super.init()
        inset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
    
    override func numberOfItems() -> Int {
        return 1
    }
    
    override func sizeForItem(at index: Int) -> CGSize {
        guard let context = collectionContext else {return .zero}
        let width = context.containerSize.width
        return CGSize(width: width, height: 100)
    }
    override func cellForItem(at index: Int) -> UICollectionViewCell {
        let cell = collectionContext!.dequeueReusableCell(of: HistoryOrderCollectionViewCell.self, for: self, at: index)
        if let cell = cell as? HistoryOrderCollectionViewCell {
            cell.initValue(order: historyOrder)
            cell.layoutSubviews()
        }
        return cell
    }
    override func didUpdate(to object: Any) {
        historyOrder = object as? UserOrder
    }
    
    override func didSelectItem(at index: Int) {
        if let roles = historyOrder.roleInOrder {
            var rs: [RoleInOrder] = []
            if roles.contains("CUSTOMER") {
                rs.append(.customer)
            }
            if roles.contains("RECOMMENDER") {
                rs.append(.recommender)
            }
            if roles.contains("MANAGER") {
                rs.append(.manager)
            }
            delegate?.showOrderDetail(order: historyOrder, role: rs)
        }
    }
}
